package com.naiterui.ehp.bs.recommend.controller.inner;

import com.naiterui.ehp.bp.bo.recommend.DrBaseBusinessDataBO;
import com.naiterui.ehp.bp.bo.recommend.DrPointPrescriptionBO;
import com.naiterui.ehp.bp.bo.recommend.MedicationRecomBO;
import com.naiterui.ehp.bp.bo.recommend.MedicationRecomDetailBO;
import com.naiterui.ehp.bp.bo.recommend.PrescriptionPlatformRecomInfoBO;
import com.naiterui.ehp.bp.bo.recommend.RecomOrderInfoBO;
import com.naiterui.ehp.bp.bo.recommend.RecomPrescriptionBO;
import com.naiterui.ehp.bp.bo.recommend.param.RecomInfoParamBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bs.recommend.exception.ExceptionCodes;
import com.naiterui.ehp.bs.recommend.service.IRecomAuditOperationLogService;
import com.naiterui.ehp.bs.recommend.service.IRecomCreaterService;
import com.naiterui.ehp.bs.recommend.service.IRecomDetailService;
import com.naiterui.ehp.bs.recommend.service.IRecomService;
import com.naiterui.ehp.bs.recommend.swagger.Tags;
import com.naiterui.ehp.bs.recommend.vo.MedicationRecomDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Api(tags = {SwaggerTag.INNER_API + SwaggerTag.RECOM})
@RequestMapping("recommend/inner")
@RestController
//@ApiIgnore
public class InnerRecomController {

    private static final Logger LOGGER = LoggerFactory.getLogger(InnerRecomController.class);

    @Autowired
    IRecomService recomService;
    @Autowired
    IRecomDetailService recomDetailService;
    @Autowired
    IRecomCreaterService recomCreaterService;
    @Autowired
    IRecomAuditOperationLogService recomAuditOperationLogService;

    /**
     * 获取推荐单信息
     * InnerIntegralController.getRecom()
     *
     * @param recomId
     *
     * @return
     *
     * @throws BusinessException
     * @Author mudou
     * @Date 2017年8月28日
     * @since 2.9.0
     */

    @RequestMapping("recom/get")
    @ResponseBody
    public ResponseVO<MedicationRecomBO> getRecom(Long recomId) throws BusinessException {
        if (null == recomId) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        MedicationRecomBO recomVO = this.recomService.getRecomVO(recomId);
        return PublicService.returnResponseVO(recomVO);
    }

    /**
     * 批量获取推荐单信息
     * InnerIntegralController.getRecomList()
     *
     * @param recomIds
     *
     * @return
     *
     * @throws BusinessException
     * @Author mudou
     * @Date 2017年8月28日
     * @since 2.9.0
     */

    @ApiOperation(tags = { Tags.INNER_RECOM }, value = "批量获取推荐单信息", notes = "批量获取推荐单信息", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "recomIds", value = "推荐ID集合", required = true, dataType = "strig", paramType = "query") })
    @RequestMapping("recom/list/get")
    @ResponseBody
    public ResponseVO<List<MedicationRecomBO>> getRecomList(String recomIds) throws BusinessException {

        if (StringUtils.isBlank(recomIds)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        Set<Long> recomIdSet = new HashSet<>(StringUtil.convertLongStr2List(recomIds, ","));
        List<MedicationRecomBO> recomVOList = this.recomService.getMedicationRecomByIds(recomIdSet);
        return PublicService.returnResponseVO(recomVOList);
    }

    /**
     * 推荐单成单状态同步
     * <p>
     * InnerIntegralController.updateOrderRecom()
     *
     *
     * @return
     *
     * @throws BusinessException
     * @Author gongbaoqiang
     * @Date 2018年10月19日
     * @since 3.4.0
     */

    @ApiOperation(tags = { Tags.INNER_RECOM }, value = "推荐单成单状态同步", notes = "推荐单成单状态同步", httpMethod = "POST")
    @PostMapping("recom/order/update/new")
    @ResponseBody
    public ResponseVO<Void> updateOrderRecom(@RequestBody RecomOrderInfoBO orderInfoBO) throws
            BusinessException {
        if (orderInfoBO == null) {
            LOGGER.error("更新推荐购买状态参数为空。");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        List<RecomInfoParamBO> recominfo = orderInfoBO.getRecomInfo();
        if (CollectionUtils.isEmpty(recominfo)) {
            LOGGER.error("更新推荐购买状态参数的推荐信息为空：recominfo=null");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        LOGGER.info("更新推荐购买状态参数, orderInfoBO={}", orderInfoBO.toString());
        this.recomService.updateOrderRecom(orderInfoBO);

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 获取推荐详情信息
     * InnerIntegralController.getRecomDetailList()
     *
     * @param recomIds
     *
     * @return
     *
     * @throws BusinessException
     * @Author mudou
     * @Date 2017年8月28日
     * @since 2.9.0
     */

    @ApiOperation(tags = { Tags.INNER_RECOM }, value = "获取推荐详情信息", notes = "获取推荐详情信息", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "recomIds", value = "推荐ID集合", required = true, dataType = "string", paramType = "query") })
    @PostMapping("recomdetail/list/get")
    @ResponseBody
    public ResponseVO<List<MedicationRecomDetailBO>> getRecomDetailList(String recomIds) throws BusinessException {

        if (StringUtils.isBlank(recomIds)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        Set<Long> recomIdSet = new HashSet<>(StringUtil.convertLongStr2List(recomIds, ","));
        List<MedicationRecomDetailVO> recomVOList = this.recomDetailService.getDetailsByRecomIds(recomIdSet);
        List<MedicationRecomDetailBO> recomDetailBOList = new ArrayList<>();
        BeanUtil.copyList(recomVOList, recomDetailBOList, MedicationRecomDetailBO.class);
        return PublicService.returnResponseVO(recomDetailBOList);
    }

    /**
     * 批量获取推荐详情信息
     * InnerIntegralController.getPrescriptionList()
     *
     * @param orderId
     * @param doctorId
     *
     * @return
     *
     * @throws BusinessException
     * @Author mudou
     * @Date 2017年8月28日
     * @since 1.0.0
     */

    @RequestMapping("prescription/list/get")
    @ResponseBody
    public ResponseVO<List<DrPointPrescriptionBO>> getPrescriptionList(Long orderId, Long doctorId, String recomIds) throws BusinessException {

        if (StringUtils.isBlank(recomIds)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        LOGGER.info("inner批量获取推荐详情信息，recomIds={}", recomIds);
        List<DrPointPrescriptionBO> recomVOList = this.recomService.getPrescriptionList(orderId, doctorId, recomIds);
        return PublicService.returnResponseVO(recomVOList);
    }

    /**
     * 根据推荐ID获取医生列表
     * InnerIntegralController.getRecomDoctorIds()
     *
     * @param recomIds
     *
     * @return
     *
     * @throws BusinessException
     * @Author guoyongxiang
     * @Date 2017年9月21日
     * @since 2.11.0
     */

    @RequestMapping("recom/doctor/list")
    @ResponseBody
    public ResponseVO<List<Long>> getRecomDoctorIds(String recomIds) throws BusinessException {
        if (recomIds == null || "".equals(recomIds)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        Set<Long> recomIdSet = StringUtil.convertLongStr2Set(recomIds, ",");
        Set<Long> doctorIdSet = this.recomService.getRecomDoctorInviter(recomIdSet);
        List<Long> doctorIdList = new ArrayList<>(doctorIdSet);

        return PublicService.returnResponseVO(doctorIdList);
    }

    @RequestMapping("generate/pdf")
    @ResponseBody
    public ResponseVO<List<String>> generateRecomPdf(String hospitalName, String recomId) throws BusinessException {
        if (recomId == null || "".equals(recomId)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        Set<Long> recomIdSet = StringUtil.convertLongStr2Set(recomId, ",");
        List<String> urls = this.recomCreaterService.generateRecomPdf(hospitalName, recomIdSet);
        return PublicService.returnResponseVO(urls);
    }

    /**
     * 获取医生业务基础数据
     * @param doctorId
     * @return
     * @throws BusinessException
     */
    @GetMapping("/doctor/business/data")
    public ResponseVO<DrBaseBusinessDataBO> getDrBaseBusinessData(@RequestParam("doctorId") Long doctorId)
            throws BusinessException {
        return PublicService.returnResponseVO(recomService.getDrBaseBusinessData(doctorId));
    }

    /**
     * 获取处方流转平台所需处方信息
     * @param recomId
     * @return
     */
    @GetMapping("platform/recom/info")
    @ResponseBody
    public ResponseVO<PrescriptionPlatformRecomInfoBO> getPlatformRecomInfo(Long recomId) throws BusinessException {
        return PublicService.returnResponseVO(recomService.getPlatformRecomInfo(recomId));
    }

    /**
     * 新增处方审核操作日志
     * @return
     */
    @PostMapping("recom/addLog")
    @ResponseBody
    public ResponseVO<Void> recomAddlog(@RequestParam("recomId") Long recomId, @RequestParam("operator") String operator,
                                        @RequestParam("type") Integer type, @RequestParam("name") String name) throws BusinessException {
        recomAuditOperationLogService.addLog(recomId, operator, type,name);
        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);
    }

    /**
     * 获取类型
     *
     * @param recomId
     * @return
     */
    @GetMapping("/recom/getType")
    @ResponseBody
    public ResponseVO<Integer> getRecommendTypeById(Long recomId) {
        return PublicService.returnResponseVO(recomService.getRecommendTypeById(recomId));
    }

    /**
     * 根据sessionId获取处方列表
     *
     * @param consultSessionIds
     * @return
     * @throws BusinessException
     */
    @GetMapping("recom/list/session")
    @ApiOperation(value = "根据会话ID批量获取推荐单信息", notes = "根据会话ID批量获取推荐单信息", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "consultSessionIds", value = "会话ID集合", required = true,
            dataType = "String", paramType = "query")})
    public ResponseVO<List<MedicationRecomBO>> getRecomSessionList(String consultSessionIds) throws BusinessException {

        if (StringUtils.isBlank(consultSessionIds)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        Set<Long> consultSessionIdSet = new HashSet<>(StringUtil.convertLongStr2List(consultSessionIds, ","));
        List<MedicationRecomBO> recomVOList =
                this.recomService.getMedicationRecomByConsultSessionIds(consultSessionIdSet);
        return PublicService.returnResponseVO(recomVOList);
    }

    /**
     * 获取处方文件
     *
     * @param consultSessionIds
     * @return
     * @throws BusinessException
     */
    @PostMapping("recom/prescription")
    @ApiOperation(value = "获取处方文件信息", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "recomIds", value = "处方id集合", required = true, dataType = "String",
            paramType = "query"),})
    public ResponseVO<List<RecomPrescriptionBO>> recomPrescriptionList(@RequestBody List<Long> recomIdList)
            throws BusinessException {
        LOGGER.info("获取处方文件信息 recomIds:{}", recomIdList);
        if (CollectionUtils.isEmpty(recomIdList)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        List<RecomPrescriptionBO> recomPrescriptionList = recomService.recomPrescriptionList(recomIdList);
        return PublicService.returnResponseVO(recomPrescriptionList);
    }

}
